<?php
namespace app\admin\controller;

use app\common\model\Fight as FightModel;
use app\common\model\Game as GameModel;
use app\common\model\GameJoin;

class Fight extends Base
{
    /**
     * 列表
     * @return \think\response\Json
     */
    public function game_list()
    {
        if (request()->isPost()) {
            $data = input('');
            $page = input('page', 1);
            $size = input('size', 20);
            $data['status'] = input('status','all');
            $where = [];
            if ($data['name']) {
                $where['name'] = ['like', "%{$data['name']}%"];
            }
            if($data['status']!='all'){
                $where['status'] = $data['status'];
            }
            $list = GameModel::getPage($where, $page, $size,'start_time desc');
            if($list){
                foreach ($list as $key => $value){
                    if($value['start_time']>date("Y-m-d H:i:s")){
                        $list[$key]['game_status'] ='未举办';
                    }else if(date('Y-m-d H:i:s')>$value['end_time']){
                        $list[$key]['game_status'] = '已举办';
                    }else{
                        $list[$key]['game_status'] = '举办中';
                    }
                }
            }
            $total = GameModel::getCount($where);
            return returnJson(200,'',compact('page','size','list','total'));
        }
    }

    public function team_list()
    {
        if(request()->isPost()){
            $data = input('');
            if($data['gid']){
                $fids = GameJoin::getColumn(['gid'=>$data['gid']],'fid','id asc');
                $where = [];
                if($fids){
                    $where['id'] = ['in',$fids];
                    $list = FightModel::getAll($where);
                }else{
                    $list = [];
                }
                return returnJson(200,'',$list);

            }
            return returnJson(202,'参数错误');
        }
    }


    /**
     * @return \think\response\Json
     */
    public function del_fight () {
        $input = input();
        if(!$input['id']) {
            return returnJson(1,'error');
        }
        \app\common\model\Game::destroy(['id'=>$input['id']]);
        return returnJson(0,'删除成功');
    }

    /**
     * @return array
     */
    public function shenhe()
    {
        if(request()->isPost()){
            $data = input('');
            if($data['id']){
                $where = ['id'=>$data['id']];
                $save_data = [
                    'status' =>$data['status']
                ];
                $res = GameModel::update($save_data,$where);
                if($res){
                    return returnJson(200,'审核成功');
                }else{
                    return returnJson(202,'审核失败');
                }
            }
        }
    }


   
}

